package com.it.service;

import com.it.mapper.SysFoodMapper;
import com.it.mapper.SysImageMapper;
import com.it.pojo.SysFood;
import com.it.pojo.SysImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class SysFoodServiceImpl implements SysFoodService {

	//由spring创建对象并为变量赋值
	@Autowired
	private SysFoodMapper sysFoodMapper;

	@Autowired
	private SysImageMapper sysImageMapper;

	/**
	 * 1.查询所有菜品信息
	 * @return
	 */
	public List<SysFood> findAll(){
		//1.调用foodMapper的findAll方法查询所有菜品
		List<SysFood> list = sysFoodMapper.findAll();
		for(SysFood food : list){
			Map<String, Object> linkMap = new HashMap<String, Object>();
			linkMap.put("linkId", food.getId());
			linkMap.put("linkType", 2);
			List<SysImage> sysImage = sysImageMapper.findByLink(linkMap);
			if(sysImage != null && sysImage.size() > 0){
				food.setSysImage(sysImage.get(0));
			}
		}
		return list;
	}
	
	/**
	 * 2.根据id删除菜品信息
	 * @param id
	 */
	public int deleteById(String id){
		//1.调用FoodMapper的deleteById方法
		//根据id删除菜品信息
		int result = sysFoodMapper.deleteById(id);
		return result;
	}
	
	/**
	 * 3.添加菜品信息
	 * @param food
	 */
	public int addSysFood(SysFood food){
		food.setId(UUID.randomUUID().toString().substring(0, 32));
		food.setCreateName("系统管理员");
		food.setCreateTime(new Date());
		int result = sysFoodMapper.addSysFood(food);
		return result;
	}
	
	/**
	 * 4.根据id查询菜品信息
	 * @param id
	 * @return 
	 */
	public SysFood findById(String id){
		SysFood food = sysFoodMapper.findById(id);
		return food;
	}
	
	/**
	 * 5.根据id修改菜品信息
	 * @param food
	 */
	public int updateById(SysFood food){
		int result = sysFoodMapper.updateById(food);
		return result;
	}
	
}
